"""
Case Type   : 资源池化
Case Name   : DFX switchover操作
Create At   : 2024.01
Owner       : chen
Description :
    1、查询集群初始状态：cm_ctl query -Cvidp
    2、将所有节点实例统一从主切换到备：cm_ctl switchover -A
    3、重置节点状态为初始配置信息：cm_ctl switchover -a
Expect      :
    1、集群初始状态正常
    2、主备切换正常
    3、重置节点状态为初始配置状态正常
"""

import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
    
    def test_shared_storage(self):
        text = '-----step1:查询集群初始状态: cm_ctl query -Cvidp;' \
            ' expect: 集群初始状态正常-----'
        self.log.info(text)
        status = self.pri_sh.exec_cm_ctl('query', '-Cvidp')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)
        cur_pri_node = self.pri_sh.get_primary_node()

        text = '-----step2:将所有节点实例统一从主切换到备: ' \
            'cm_ctl switchover -A expect: 主备切换正常-----'
        self.log.info(text)
        status = self.pri_sh.exec_cm_ctl('switchover', '-A')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_A_success_msg, status)
        cur_pri_nodeA = self.pri_sh.get_primary_node()
        self.assertNotEqual(cur_pri_node, cur_pri_nodeA)
    
    def tearDown(self):
        self.log.info('-----step3:重置节点状态为初始配置信息: ' \
            'cm_ctl switchover -a expect: 重置节点状态为初始配置状态正常-----')
        status = self.pri_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')